package com.sskj.community.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.community.presenter.ArticlePresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TextDetailsBean;
import com.sskj.lib.widget.BottomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.COMMUNITY_ACTIVITY_ARTICLE)
public class ArticleActivity extends BaseActivity<ArticlePresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_date)
    TextView tvDate;
    @BindView(R2.id.tv_content)
    TextView tvContent;
    @BindView(R2.id.tv_share)
    TextView tvShare;
    @BindView(R2.id.iv_zan)
    ImageView ivZan;
    @BindView(R2.id.tv_zan)
    TextView tvZan;
    @BindView(R2.id.ll_zan)
    LinearLayout llZan;
    @Autowired
    String like_num;
    @Autowired
    int Is_like;
    @Autowired
    String msg_id;
    private boolean flag = true;

    @Override
    public int getLayoutId() {
        return R.layout.community_activity_article;
    }

    @Override
    public ArticlePresenter getPresenter() {
        return new ArticlePresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        if (Is_like == 1) {
            flag = true;
        } else {
            flag = false;
        }
        tvTitle.setText(App.INSTANCE.getString(R.string.community_articleActivity1));
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.getTextDetails(msg_id);
    }

    @Override
    public void initData() {
        ClickUtil.click(300, tvShare, () -> {
            ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_SHARE)
                    .withString("msg_id", msg_id)
                    .navigation();
        });
        ClickUtil.click(300, llZan, () -> {
            if (flag) {
                mPresenter.goLike("0", msg_id,false);
                flag = false;
            } else {
                mPresenter.goLike("1", msg_id,false);
                flag = true;
            }
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ArticleActivity.class);
        context.startActivity(intent);
    }

    public void getTextDetails(TextDetailsBean data) {
        if (data != null) {
            tv_title.setText(data.getTitle());
            Glide.with(ivHead).load(data.getUser_pic()).into(ivHead);
            ImageUtil.setImage(R.mipmap.icon_head, ivHead);
            if (data.getIs_like() == 0) {
                ivZan.setEnabled(false);
            } else {
                ivZan.setEnabled(true);
            }
            tvShare.setText(data.getShare_num());
            tvZan.setText(data.getLike_num());
            tvName.setText(data.getUsername());
            tvDate.setText(TimeFormatUtil.formatE(Long.valueOf(data.getAddtime()) * 1000));
            tvContent.setText(data.getContent());
        }
        method();
    }

    public synchronized void method() {
        this.notify();
    }

    @SuppressLint("SetTextI18n")
    public void setLikeSuccess() {
        //flag根据上个界面传递过来的is_like（是否点过赞）进行判断，改变图标
        if (flag) {
            if (Is_like==0){
                int i = Integer.parseInt(like_num);
                i++;
                tvZan.setText(i + "");
            }else {
                tvZan.setText(like_num);
            }
            ivZan.setEnabled(true);

        } else {
            if (Is_like==0){
                tvZan.setText(like_num);
            }else {
                int i = Integer.parseInt(like_num);
                i--;
                tvZan.setText(i + "");
            }
            ivZan.setEnabled(false);
        }
    }
}
